package com.example.demo.zookeeper.mytest.config;

import org.apache.zookeeper.ZooKeeper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.concurrent.TimeUnit;

/**
 * @author liweizhi
 * @date 2020/6/23
 */
public class TestConfig {
    ZooKeeper zk;
    MyConf conf;

    @Before
    public void connect() {
        zk = ZkUtil.initZookeeper();
    }

    @Test
    public void testConfig() {
        conf = new MyConf();
        WatcherCallback wcb = new WatcherCallback();
        wcb.setZk(zk);
        wcb.setConf(conf);

        wcb.aWait();

        // 模拟业务端调用
        while (true) {
            if (conf.getConf() == null || "".equals(conf.getConf())) {
                wcb.aWait();
            }

            System.out.println(conf.getConf());
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @After
    public void close() {
        try {
            zk.close();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
